<?php
	class crud_ntlivescore {
		function getAllNtLiveScore(){
			$sql = "select
						ls.*,
						gc.NAME,
						hgc.ID as HOLE_GOLF_COURSE_ID,
						hgc.HOLE,
						hgc.`LENGTH`,
						hgc.PAR,
						hs.ID HOLE_SCORE_ID,
						hs.SCORE
					from live_score ls
					left join golf_course gc on gc.ID = ls.GOLF_COURSE_ID
					left join hole_golf_course hgc on hgc.GOLF_COURSE_ID = gc.ID
					left join hole_score hs on hgc.ID = hs.HOLE_GOLF_COURSE_ID and hs.LIVE_SCORE_ID = ls.ID where 1=1 and ls.TEE_TIME_ID = ".$_POST['id_teetime']."";
			$sqlCount = "select 
							count(*) as total
					from live_score ls
					left join golf_course gc on gc.ID = ls.GOLF_COURSE_ID
					left join hole_golf_course hgc on hgc.GOLF_COURSE_ID = gc.ID
					left join hole_score hs on hgc.ID = hs.HOLE_GOLF_COURSE_ID and hs.LIVE_SCORE_ID = ls.ID where 1=1 and ls.TEE_TIME_ID = ".$_POST['id_teetime']."";
			
			
			$resultCount = Yii::app()->db->createCommand($sqlCount)->queryAll();
			$sql1 = $sql." order by hgc.HOLE LIMIT ".($resultCount[0]['total']/2)." OFFSET 0";
			$sql2 = $sql." order by hgc.HOLE LIMIT ".($resultCount[0]['total']/2)." OFFSET ".($resultCount[0]['total']/2);
			// echo $sql2;exit;
			$resultSql = Yii::app()->db->createCommand($sql1)->queryAll();
			$resultSql2 = Yii::app()->db->createCommand($sql2)->queryAll();
			
			
			$response = array(
				'columndata'=> array(
					// array('id' => 'ID', 'header' => 'ID', 'width' => 30, 'sortable' => true, 'dataIndex' => 'ID'),
					// array('header' => 'Name', 'width' => 100, 'dataIndex' => 'NAME'),
					// array('header' => 'PAR', 'width' => 250, 'dataIndex' => 'PAR'),
					array('header' => 'No', 'width' => 50, 'dataIndex' => 'NO'),
					array('header' => 'Description', 'width' => 100, 'dataIndex' => 'DESCRIP'),
				),
				'fielddata'=> array(
					array('name' => 'NO', 'type'=>'INT'),
					array('name' => 'DESCRIP'),
				),
				'values'=> array(
					array(1,'Par'),
					array(2,'Score')
				),
				'columndata2'=> array(
					// array('id' => 'ID', 'header' => 'ID', 'width' => 30, 'sortable' => true, 'dataIndex' => 'ID'),
					// array('header' => 'Name', 'width' => 100, 'dataIndex' => 'NAME'),
					// array('header' => 'PAR', 'width' => 250, 'dataIndex' => 'PAR'),
					array('header' => 'No', 'width' => 50, 'dataIndex' => 'NO'),
					array('header' => 'Description', 'width' => 100, 'dataIndex' => 'DESCRIP'),
				),
				'fielddata2'=> array(
					array('name' => 'NO', 'type'=>'INT'),
					array('name' => 'DESCRIP'),
				),
				'values2'=> array(
					array(1,'Par'),
					array(2,'Score')
				),
			);
			foreach ($resultSql as $val => $r){
				array_push($response['columndata'], array(
						'header' => "Hole ".$r['HOLE'], 'width' => 100, 'dataIndex' => "HOLE_".$r['HOLE'],'align' => 'center',
						'editor' => array(
							'xtype' => 'numberfield',
							'allowBlank' => false,
							'minValue' => 0,
							'maxValue' => 100000
						)
					)
				);
				
				array_push($response['columndata'], array(
						'header' => "Hole ".$r['HOLE_GOLF_COURSE_ID'], 'width' => 100, 'dataIndex' => "HOLE_ID_".$r['HOLE_GOLF_COURSE_ID'],'align' => 'center',
						'hidden'  => true,
						'editor' => array(
							'xtype' => 'numberfield',
							'allowBlank' => false,
							'minValue' => 0,
							'maxValue' => 100000
						)
					)
				);
				
				array_push($response['fielddata'], array(
						'name' => "HOLE_".$r['HOLE'], 'type' => 'int'
					)
				);
				
				array_push($response['fielddata'], array(
						'name' => "HOLE_ID_".$r['HOLE_GOLF_COURSE_ID'], 'type' => 'int'
					)
				);
			}
			$i = 0;
			foreach ($resultSql as $val => $r){
				array_push($response['values'][0],$r['PAR']);
				array_push($response['values'][0],0);
				array_push($response['values'][1],$r['SCORE']);
				array_push($response['values'][1],$r['HOLE_GOLF_COURSE_ID']);
				$i++;
			}
			
			foreach ($resultSql2 as $val => $r){
				array_push($response['columndata2'], array(
						'header' => "Hole ".$r['HOLE'], 'width' => 100, 'dataIndex' => "HOLE_".$r['HOLE'],'align' => 'center',
						'editor' => array(
							'xtype' => 'numberfield',
							'allowBlank' => false,
							'minValue' => 0,
							'maxValue' => 100000
						)
					)
				);
				
				array_push($response['columndata2'], array(
						'header' => "Hole ".$r['HOLE_GOLF_COURSE_ID'], 'width' => 100, 'dataIndex' => "HOLE_ID_".$r['HOLE_GOLF_COURSE_ID'],'align' => 'center',
						'hidden'  => true,
						'editor' => array(
							'xtype' => 'numberfield',
							'allowBlank' => false,
							'minValue' => 0,
							'maxValue' => 100000
						)
					)
				);
				
				array_push($response['fielddata2'], array(
						'name' => "HOLE_".$r['HOLE'], 'type' => 'int'
					)
				);
				
				array_push($response['fielddata2'], array(
						'name' => "HOLE_ID_".$r['HOLE_GOLF_COURSE_ID'], 'type' => 'int'
					)
				);
			}
			$i = 0;
			foreach ($resultSql2 as $val => $r){
				array_push($response['values2'][0],$r['PAR']);
				array_push($response['values2'][0],0);
				array_push($response['values2'][1],$r['SCORE']);
				array_push($response['values2'][1],$r['HOLE_GOLF_COURSE_ID']);
				$i++;
			}
			
			echo json_encode($response);
		}
		
		function insert(){
			$model = new GolfCourse;
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$model->NAME = $_POST['nama-inputEl'];
				$model->TEE_BOX_ID = $_POST['id_teebox-inputEl'];
				$model->save();
				$transaction->commit();
				$feedback = '{"feedback":[{"message":"Insert Sukses","code":"1","ID":"'.$model->ID.'"}]}';
			} catch (Exception $e) {
				// echo $e;
				$transaction->rollback();
				$feedback = '{"feedback":[{"message":"Insert Gagal","code":"0"}]}';
			}
			echo $feedback;
		}
		
		function update(){
			$model = GolfCourse::model()->findByPk($_POST['id-inputEl']);
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$model->NAME = $_POST['nama-inputEl'];
				$model->TEE_BOX_ID = $_POST['id_teebox-inputEl'];
				$model->save();
				$transaction->commit();
				$feedback = '{"feedback":[{"message":"Update Sukses","code":"1","ID":"'.$model->ID.'"}]}';
			} catch (Exception $e) {
				// echo $e;
				$transaction->rollback();
				$feedback = '{"feedback":[{"message":"Update Gagal","code":"0"}]}';
			}
			echo $feedback;
		}
		
		function delete(){
			$model = GolfCourse::model()->findByPk($_POST['id']);
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$model->delete();
				$transaction->commit();
				$feedback = '{"feedback":[{"message":"Golf Course : '.$model->NAME.' Berhasil Dihapus","code":"1","ID":"'.$model->ID.'"}]}';
			} catch (Exception $e) {
				// echo $e;
				$transaction->rollback();
				$feedback = '{"feedback":[{"message":"Golf Course : '.$model->NAME.' Gagal Dihapus","code":"0"}]}';
			}
			echo $feedback;
		}
	}
?>